#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QTextCodec>
#include <QFileInfo>
#include <QDebug>
#include <QDateTime>
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
//点击按钮，将打开的文件路径写入到line，内容写到textEdit
    connect(ui->pushButton, &QPushButton::clicked, [ = ]() {
        QString path = QFileDialog::getOpenFileName(this, "打开文件", "C:\\Users\\Administrator\\Desktop");
//将路径放入line
        ui->lineEdit->setText(path);
//编码格式类
        QTextCodec *codec = QTextCodec::codecForName("utf-8"); //默认就是utf-8
//d读取文件内容放入到textEdit
        QFile file(path);
        file.open(QIODevice::ReadOnly);//设置打开方式
//QByteArray array=file.readAll();//全部读取
        QByteArray array;
        while(!file.atEnd()) { //读到末尾为止
            array += file.readLine(); //按行读
        }
        // ui->textEdit->setText(array);
        ui->textEdit->setText(codec->toUnicode(array));
        file.close();
        //写文件
        // file.open(QIODevice::Append);
        // file.write("22222222");
        // file.close();
        //获取文件信息
        QFileInfo info(path);
        qDebug() << "大小" << info.size() << " 后缀名：" << info.suffix() << " 文件名" << info.fileName() << " 文件路径" << info.path();
        qDebug() << "创建日期" << info.created().toString("yyyy/MM/dd hh:mm:ss");
        qDebug() << "修改日期" << info.lastModified().toString("yyyy-MM-dd hh:mm:ss");
    });
}
Widget::~Widget()
{
    delete ui;
}
